//
//  EPWebV2ViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/28.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "EPWebV2ViewModel.h"

#import "OfflineFileDBData.h"

@interface EPWebV2ViewModel ()

@property (nonatomic, readwrite, copy) NSString* fileType;
@property (nonatomic, readwrite, copy) NSString* fileName;
@property (nonatomic, readwrite, copy) NSString* fileDate;
@property (nonatomic, readwrite, copy) NSString* fileUrl;

@end

@implementation EPWebV2ViewModel

#pragma mark - Ovrride
- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    [[[RACObserve(self, data) distinctUntilChanged] filter:^BOOL(id value) {
        return value!=nil;
    }] subscribeNext:^(id x)
    {
        @strongify(self);
        
        OfflineFileDBData* curData = x;
        
        UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
        
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        
        NSArray* strSeparates = [curData.fileUrl componentsSeparatedByString:@"/"];
        NSString* fileName = strSeparates.lastObject;
        
        NSString* strPath = @"";
        strPath = [strPath stringByAppendingString:um.userData.userID];
        strPath = [strPath stringByAppendingString:@"/"];
        strPath = [strPath stringByAppendingString:fileName];
        
        NSString* fileSavePath = [documentsDirectory stringByAppendingPathComponent:strPath];
        
        if ([fileName containsString:@"doc"])
        {
            self.fileType = @"docx";
        }
        else if ([fileName containsString:@"pdf"])
        {
            self.fileType = @"pdf";
        }
        
        self.fileName = curData.name;
        self.fileUrl = fileSavePath;
        self.fileDate = curData.date;
    }];
}

@end
